package com.wims.data;

import java.util.List;

import javax.jdo.PersistenceManager;

public class WIMSDatabase {
	
	
	/**
	 * Creates all asset types (persons, documents) and saves them into the DB
	 * 
	 * @author ivananca
	 */
	public void initDataTypes() {
		cleanup();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		initPersonTypes(pm);
		initDocumentTypes(pm);
		initLifeEventTypes(pm);
		
		pm.close();
	}


	/**
	 * Creates person types and saves them into the DB
	 * @param pm
	 * 
	 * @author anca ivan
	 */
	private void initPersonTypes(PersistenceManager pm) {
		
		PersonType parent = new PersonType("Parent");
		PersonType child = new PersonType("Child");
		PersonType sibling = new PersonType("Sibling");
		PersonType relative = new PersonType("Relative");
		PersonType other = new PersonType("Other");
		
        try {
            pm.makePersistent(parent);
            pm.makePersistent(child);
            pm.makePersistent(sibling);
            pm.makePersistent(relative);
            pm.makePersistent(other);
        } catch(Exception e) {
            e.printStackTrace();
        }
	}
	
	
	/*
	 * Creates document types and saves them into the DB
	 * 
	 * @param pm
	 * 
	 * @author anca ivan
	 */
	private void initDocumentTypes(PersistenceManager pm) {
		
		DocumentType healthIns = new DocumentType("Health Insurance");
		DocumentType carIns = new DocumentType("Car Insurance");
		DocumentType homeIns = new DocumentType("Home Insurance");
		DocumentType renterIns = new DocumentType("Renter Insurance");
		
		try {
            pm.makePersistent(healthIns);
            pm.makePersistent(carIns);
            pm.makePersistent(homeIns);
            pm.makePersistent(renterIns);
        } catch(Exception e) {
        	e.printStackTrace();
        }
	}
	
	
	/*
	 * Creates life event types and saves them into the DB
	 * 
	 * @param pm
	 * 
	 * @author anca ivan
	 */
	private void initLifeEventTypes(PersistenceManager pm) {
		
		LifeEventType birth = new LifeEventType("Give birth");
		LifeEventType wedding = new LifeEventType("Get married");
		LifeEventType buyHouse = new LifeEventType("Buy a house");
		LifeEventType buyCar = new LifeEventType("Buy a car");
		LifeEventType goCollege = new LifeEventType("Go to college");
		LifeEventType sellHouse = new LifeEventType("Sell a house");
		LifeEventType death = new LifeEventType("Death");

		
        try {
            pm.makePersistent(birth);
            pm.makePersistent(wedding);
            pm.makePersistent(buyHouse);
            pm.makePersistent(buyCar);
            pm.makePersistent(goCollege);
            pm.makePersistent(sellHouse);
            pm.makePersistent(death);
        } catch(Exception e) {
            e.printStackTrace();
        }
	}
	
	
	/**
	 * Deletes all asset types from the DB
	 * 
	 * @author anca ivan
	 */
	public void cleanup() {
		//anca: get all asset types and delete them
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + PersonType.class.getName();
	    List<ObjectType> assetTypes = (List<ObjectType>) pm.newQuery(query).execute();
	    
	    pm.deletePersistentAll(assetTypes);
	    assetTypes = (List<ObjectType>) pm.newQuery(query).execute();
	    assert(assetTypes.isEmpty());	    
	}
}
